/**
 *
 */
package ua.at.tsvetkov.samizdat.adapters;

import java.util.ArrayList;

import ua.at.tsvetkov.samizdat.R;
import ua.at.tsvetkov.samizdat.data.Data;
import ua.at.tsvetkov.samizdat.data.FavoriteAuthors;
import ua.at.tsvetkov.strings.Util;
import ua.at.tsvetkov.util.Log;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 */
public class AdtSearchAuthors extends BaseAdapter {

	private FavoriteAuthors		favoriteAuthors;
	private LayoutInflater		mInflater;
	private ArrayList<String>	authorsData;
	private int						size;
	private Activity				context;

	/**
	 * @param context
	 * @param dataReader
	 */
	public AdtSearchAuthors(Activity context) {
		super();
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		favoriteAuthors = new FavoriteAuthors(context);
		favoriteAuthors.load();
		this.authorsData = new ArrayList<String>();
		this.size = 0;

	}

	@Override
	public int getCount() {
		return size;
	}

	@Override
	public Object getItem(int position) {
		return authorsData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		AuthorViewHolder holder = new AuthorViewHolder();
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.adt_author, null);
			holder.author = (TextView) convertView.findViewById(R.id.AdtAuthorAuthor);
			holder.title = (TextView) convertView.findViewById(R.id.AdtAuthorTitle);
			holder.annotation = (TextView) convertView.findViewById(R.id.AdtAuthorAnnotation);
			holder.count = (TextView) convertView.findViewById(R.id.AdtAuthorBookCount);
			holder.size = (TextView) convertView.findViewById(R.id.AdtAuthorSize);
			holder.favoriteAuthor = (ImageView) convertView.findViewById(R.id.AdtAuthorIsFavorite);
			convertView.setTag(holder);
		} else {
			holder = (AuthorViewHolder) convertView.getTag();
		}
		try {
			String[] fields = Util.split(authorsData.get(position), Data.AUTHOR_FIELDS_COUNT);
			holder.author.setText(fields[Data.AUTHOR_AUTHOR]);
			holder.title.setText(fields[Data.AUTHOR_TITLE].trim());
			String annotation = fields[Data.AUTHOR_ANNOT];
			if (annotation != null && annotation.length() != 0) {
				holder.annotation.setText(annotation);
			}
			holder.count.setText(fields[Data.AUTHOR_COUNT] + " шт");
			if (favoriteAuthors.isFavorite(fields[Data.AUTHOR_AUTHOR])) {
				holder.favoriteAuthor.setVisibility(View.VISIBLE);
			}
			String size = fields[Data.AUTHOR_SIZE];
			if (size != null && size.length() != 0) {
				holder.size.setText(size + " кб");
			}
		} catch (Exception e) {
			Log.e("Line " + position + ":" + authorsData.get(position), e);
		}
		return convertView;
	}

	class AuthorViewHolder {
		TextView		author;
		TextView		title;
		TextView		annotation;
		TextView		size;
		TextView		count;
		ImageView	favoriteAuthor;
	}

	public void refreshData(ArrayList<String> authorsData) {
		this.authorsData = authorsData;
		this.size = authorsData.size();
		favoriteAuthors.clear();
		favoriteAuthors.load();
		context.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				AdtSearchAuthors.this.notifyDataSetChanged();
			}
		});

	}

}
